/*
* Copyright (C) 2010-2011 dppc.org
* All rights reserved.
*/
package com.seemsea.web.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.seemsea.cache.CacheMap;
import com.seemsea.entity.admin.coupon.Coupon;
import com.seemsea.entity.admin.coupon.CouponItem;
import com.seemsea.entity.admin.coupon.Product;
import com.seemsea.service.admin.CouponItemService;
import com.seemsea.service.admin.CouponService;
import com.seemsea.service.admin.ProductService;
import com.seemsea.utils.ControllerUtils;

/**
 *优惠券控制器
 *
 *@author chirowong
 *@version 1.0 2011-6-14
 *@since 1.0
 */
@Controller
public class AppCouponController {
	@Autowired
	private CouponService couponService;
	@Autowired
	private CouponItemService couponItemService;
	@Autowired
	private ProductService productService;
	@Autowired
	private CacheMap<String,List<CouponItem>> cacheMap;
	
	@RequestMapping(value="/app/coupon.nhtml")
    public String init(Model model,HttpServletRequest request) {
		String type = request.getParameter("type"); 
		if(type == null || "".equals(type)){
			type = "seemsea";
		}
		String productCode = request.getParameter("p");
		if(productCode == null || "".equals(productCode)){
			productCode = "P000001";
		}
		List<Product> products = productService.getAll();
		model.addAttribute("products", products);
		model.addAttribute("productCode", productCode);
		
		List<CouponItem> couponItems = null;
		couponItems = cacheMap.getValue(productCode);
		if(couponItems == null || couponItems.size() == 0){
			couponItems = new ArrayList<CouponItem>();
			Product product = new Product();
			product.setCode(productCode);
			List<Coupon> coupons = couponService.getCouponByProduct(product);
			if(coupons != null && coupons.size() > 0){
				for(Iterator<Coupon> iter = coupons.iterator();iter.hasNext();){
					Coupon tmpCoupon = iter.next();
					couponItems.addAll(tmpCoupon.getCouponItems());
				}
			}
		}
		if(couponItems.size() == 0){
			model.addAttribute("num", "0");
		}else{
			cacheMap.insertOrUpdate(productCode, couponItems);
			model.addAttribute("num", couponItems.size()+"");
		}
		return "/app/coupon/"+type+"/index";
    }
	
	@RequestMapping(value="/app/getCouponItem.nhtml")
	public void getCouponItem(HttpServletRequest request,HttpServletResponse response){
		String productCode = request.getParameter("productCode");//产品编码
		String currentItem = request.getParameter("current");//当前是第几张
		int _currentItem = 0;
		try{
			 _currentItem = Integer.valueOf(currentItem)-1;
		}catch(Exception e){
			_currentItem = 0;
		}
		if(productCode == null || "".equals(productCode)){
			productCode = "P000001";
		}
		List<CouponItem> couponItems = null;
		couponItems = cacheMap.getValue(productCode);
		if(couponItems == null || couponItems.size() == 0){
			couponItems = new ArrayList<CouponItem>();
			Product product = new Product();
			product.setCode(productCode);
			List<Coupon> coupons = couponService.getCouponByProduct(product);
			if(coupons != null && coupons.size() > 0){
				for(Iterator<Coupon> iter = coupons.iterator();iter.hasNext();){
					Coupon tmpCoupon = iter.next();
					couponItems.addAll(tmpCoupon.getCouponItems());
				}
			}
		}
		Map<String,String> jsonMap = new HashMap<String,String>();
		if(couponItems.size() == 0){
			jsonMap.put("flag", "nodata");
		}else{
			cacheMap.insertOrUpdate(productCode, couponItems);
			jsonMap.put("num", couponItems.size()+"");
			if(_currentItem < 0){
				jsonMap.put("flag", "first");
			}else if(_currentItem >= couponItems.size()){
				jsonMap.put("flag", "end");
			}else{
				CouponItem couponItem = couponItems.get(_currentItem);
				jsonMap.put("flag", "success");
				jsonMap.put("img", couponItem.getUrl());
				jsonMap.put("code", String.valueOf(couponItem.getCode()));
				jsonMap.put("content", couponItem.getCoupon().getName());
			}
		}
        Gson gson = new Gson();
        String graphStr = gson.toJson(jsonMap);
        ControllerUtils.responseClientWithJson(response, graphStr);
	}
	
	@RequestMapping(value="/app/showBigPic.nhtml")
    public String showBigPic(Model model,HttpServletRequest request) {
		String type = request.getParameter("type"); 
		if(type == null || "".equals(type)){
			type = "seemsea";
		}
		Integer couponItemCode = Integer.valueOf(request.getParameter("p"));
		CouponItem couponItem = couponItemService.get(couponItemCode);
		model.addAttribute("couponItem",couponItem);
		return "/app/coupon/"+type+"/showPic";
    }
}
